package cn.com.controller;

import cn.com.Util.CopyUtils;
import cn.com.domain.SuggestEntity;
import cn.com.domain.dto.SuggestDTO;
import cn.com.mapper.SuggestMapper;
import cn.com.response.BaseResponse;
import cn.com.response.BaseResponseStatus;
import cn.com.service.SuggestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 合理化建议
 *
 * @Author CRL
 * @Date 2021/4/21 21:23
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/suggest")
public class SuggestController {

    @Autowired
    SuggestService suggestService;
    @Autowired
    SuggestMapper suggestMapper;

    @PostMapping
    public BaseResponse<SuggestEntity> creat(@RequestBody(required = false) SuggestDTO requestDto) {
        SuggestEntity suggestEntity = new SuggestEntity();
        requestDto.setCreateTime(new Date());
        CopyUtils.copyProperties(requestDto, suggestEntity);
        boolean save = suggestService.save(suggestEntity);
        if (save) {
            return new BaseResponse(suggestEntity);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "保存失败");
        }
    }

    @DeleteMapping("/{id}")
    public BaseResponse delete(@PathVariable("id") String id) {
        int i = suggestMapper.deleteById(id);
        if (i == 1) {
            return new BaseResponse(BaseResponseStatus.OK, "删除成功");
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "删除失败");
        }
    }

    @PutMapping("/{id}")
    public BaseResponse update(@PathVariable("id") String id, @RequestBody(required = false) SuggestDTO requestDto) {
        SuggestEntity suggestServiceById = suggestService.getById(id);
        CopyUtils.copyProperties(requestDto, suggestServiceById);
        suggestServiceById.setUpdateTime(new Date());
        boolean b = suggestService.saveOrUpdate(suggestServiceById);
        if (b) {
            return new BaseResponse<>(suggestServiceById);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "更新失败");
        }
    }

    @PostMapping("/query")
    public BaseResponse query(@RequestBody(required = false) SuggestDTO requestDto) {
        Integer pageNo = requestDto.getPageNo();
        Integer pageSize = requestDto.getPageSize();
        IPage<SuggestEntity> userPage = new Page<>(pageNo, pageSize);
        QueryWrapper<SuggestEntity> wrapper = new QueryWrapper<>();
        CopyUtils.copyProperties(requestDto, wrapper);
        IPage<SuggestEntity> backlogEntityIPage = suggestMapper.selectPage(userPage, wrapper);
        return new BaseResponse(backlogEntityIPage);
    }

    @GetMapping("/{id}")
    public BaseResponse findOneById(@PathVariable("id") String id) {
        SuggestEntity serviceById = suggestService.getById(id);
        return new BaseResponse(serviceById);
    }

}
